package com.clean.notification.noti

import android.content.Context
import android.os.Build
import androidx.lifecycle.LifecycleCoroutineScope
import com.hjq.permissions.OnPermissionCallback
import com.hjq.permissions.Permission
import com.hjq.permissions.XXPermissions
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch

object RequestNotiPerUtils {
    private var isRequest = false

    //    private var popReqPermissions: BasePopupView? = null
    fun requestNotify(
        context: Context,
        lifecycleScope: LifecycleCoroutineScope,
        needGuide: Boolean = false,
        next: (() -> Unit)? = null
    ) {
        if (!isRequest) {
            isRequest = true
            if (!XXPermissions.isGranted(context, Permission.POST_NOTIFICATIONS)) {
                if (needGuide && Build.VERSION.SDK_INT >= 34) {
//                    popReqPermissions?.dismiss()
//                    popReqPermissions =
//                        XPopup.Builder(context).asCustom(PopReqPermissions(context) {
//                            reqPer(context, next, lifecycleScope, needGuide)
//                        }).show()
                    reqPer(context, next, lifecycleScope, needGuide)
                } else {
                    reqPer(context, next, lifecycleScope, needGuide)
                }
            } else {
//                popReqPermissions?.dismiss()
                showNotification(lifecycleScope)
                next?.invoke()
            }
        } else {
            next?.invoke()
            showNotification(lifecycleScope)
        }
    }

    private fun reqPer(
        context: Context,
        next: (() -> Unit)?,
        lifecycleScope: LifecycleCoroutineScope,
        needGuide: Boolean = false,
    ) {
        XXPermissions.with(context).permission(Permission.POST_NOTIFICATIONS)
            .request(object : OnPermissionCallback {
                override fun onGranted(p0: MutableList<String>, p1: Boolean) {
                    next?.invoke()
                    showNotification(lifecycleScope)
                }

                override fun onDenied(
                    permissions: MutableList<String>, doNotAskAgain: Boolean
                ) {
                    super.onDenied(permissions, doNotAskAgain)
                    next?.invoke()
                    showNotification(lifecycleScope)
                    if (doNotAskAgain && needGuide && Build.VERSION.SDK_INT >= 34) {
                        XXPermissions.startPermissionActivity(context, permissions)
//                        popReqPermissions?.dismiss()
                    }
                }
            })
    }

    private fun showNotification(lifecycleScope: LifecycleCoroutineScope) {
        lifecycleScope.launch(Dispatchers.IO) {
            delay(2000)
            isRequest = false
//            if (GreenNotifyManagerUtils.isNotificationEnabled()) {
//                GreenCoreMainManager.startNotifyService(true)
//            }
//            GreenCoreMainManager.tryStartLaunchMainService()
        }
    }
}